﻿using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace QT.Lib.SixLabors.ImageSharp
{
    public static class ImageUtilis
    {
        /// <summary>
        /// 单个图片加滤镜
        /// </summary>
        /// <param name="loadPath">图片地址</param>
        /// <param name="filter">滤镜</param>
        /// <param name="savePath">保存地址</param>
        /// <param name="amount">滤镜效果值</param>
        public static void Filter(string loadPath, Filter filter, string savePath, float amount = 0.5f) 
        {
            Image image = Image.Load(loadPath);

            switch (filter)
            {
                case ImageSharp.Filter.Grayscale:
                    image.Mutate(x=>x.Grayscale(amount));
                    break;
                case ImageSharp.Filter.BlackWhite:
                    image.Mutate(x => x.BlackWhite());
                    break;
                case ImageSharp.Filter.Sepia:
                    image.Mutate(x => x.Sepia(amount));
                    break;
                case ImageSharp.Filter.Invert:
                    image.Mutate(x => x.Invert());
                    break;
                case ImageSharp.Filter.GaussianBlur:
                    image.Mutate(x => x.GaussianBlur(amount));
                    break;
                case ImageSharp.Filter.GaussianSharpen:
                    image.Mutate(x => x.GaussianSharpen(amount));
                    break;
                case ImageSharp.Filter.Vignette:
                    image.Mutate(x => x.Vignette());
                    break;
                case ImageSharp.Filter.Brightness:
                    image.Mutate(x => x.Brightness(amount));
                    break;
                case ImageSharp.Filter.Contrast:
                    image.Mutate(x => x.Contrast(amount));
                    break;
                case ImageSharp.Filter.Saturate:
                    image.Mutate(x => x.Saturate(amount));
                    break;
                case ImageSharp.Filter.Hue:
                    image.Mutate(x => x.Hue(amount));
                    break;
                case ImageSharp.Filter.Pixelate:
                    image.Mutate(x => x.Pixelate());
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(filter),$"参数值：{filter}无效！");
            }
            // 获取目录的上一级目录
            DirectoryInfo parentDirectory = Directory.GetParent(savePath);
            //临时保存路径
            var tempPath = Path.Combine(parentDirectory.FullName, "Filter");
            if (!Directory.Exists(tempPath))
            {
                // 如果文件夹不存在，则创建文件夹
                Directory.CreateDirectory(tempPath);
            }

            image.Save(Path.Combine(parentDirectory.FullName, $"Filter\\{Path.GetFileName(loadPath)}"));
        }

        /// <summary>
        /// 批量图片加滤镜
        /// </summary>
        /// <param name="loadPath">图片地址列表</param>
        /// <param name="filter">滤镜</param>
        /// <param name="savePath">保存地址</param>
        /// <param name="amount">滤镜效果值</param>
        public static void Filter(List<string> loadPath, Filter filter, string savePath, float amount = 0.5f)
        {
            loadPath.ForEach(x=>
            {
                Filter(x,filter, savePath,amount);
            });
        }

        /// <summary>
        /// 单个图片加贴图
        /// </summary>
        /// <param name="loadPath">需要加贴图的图片地址</param>
        /// <param name="chartlet">贴图</param>
        /// <param name="point">贴图位置锚点</param>
        /// <param name="savePath">保存地址</param>
        /// <param name="opacity">不透明度</param>
        public static void Merge(string loadPath, string chartlet, Point point, string savePath, float opacity =1f) 
        {
            DirectoryInfo parentDirectory = Directory.GetParent(savePath);
            Image image = Image.Load(Path.Combine(parentDirectory.FullName, $"Filter/{Path.GetFileName(loadPath)}"));
            Image chartletImg = Image.Load(chartlet);
            using (Image outputImg = new Image<Rgba64>(image.Width, image.Height))
            {
                outputImg.Mutate(x => x.DrawImage(image, new Point(0, 0), 1f));
                outputImg.Mutate(x => x.DrawImage(chartletImg, point, opacity));
                //临时保存路径
                var tempPath = Path.Combine(parentDirectory.FullName, "Merge");
                if (!Directory.Exists(tempPath))
                {
                    // 如果文件夹不存在，则创建文件夹
                    Directory.CreateDirectory(tempPath);
                }
                outputImg.Save(Path.Combine(parentDirectory.FullName, $"Merge/{Path.GetFileName(loadPath)}"));
            }
        }

        /// <summary>
        /// 批量图片加贴图
        /// </summary>
        /// <param name="images">需要加贴图的图片地址集合</param>
        /// <param name="chartlet">贴图</param>
        /// <param name="point">贴图位置锚点</param>
        /// <param name="savePath">保存地址</param>
        /// <param name="opacity">不透明度</param>
        public static void Merge(List<string> loadPath, string chartlet, Point point, string savePath, float opacity = 1f)
        {
            loadPath.ForEach(x =>
            {
                Merge(x,chartlet, point,savePath, opacity);
            });
        }

        /// <summary>
        /// 图片添加水印
        /// </summary>
        /// <param name="loadPath">需要添加水印的图片地址</param>
        /// <param name="text">水印文字</param>
        /// <param name="font">字体</param>
        /// <param name="color">颜色</param>
        /// <param name="point">水印位置</param>
        public static void Watermark(string loadPath, string text, Font font, Color color, PointF point, string savePath) 
        {
            DirectoryInfo parentDirectory = Directory.GetParent(savePath);
            Image image = Image.Load(Path.Combine(parentDirectory.FullName, $"Merge/{Path.GetFileName(loadPath)}"));
            image.Mutate(x=>x.DrawText(text,font,color,point));
            image.Save(Path.Combine(savePath, Path.GetFileName(loadPath)));
        }

        /// <summary>
        /// 批量图片添加水印
        /// </summary>
        /// <param name="loadPath">需要添加水印的图片地址集合</param>
        /// <param name="text">水印文字</param>
        /// <param name="font">字体</param>
        /// <param name="color">颜色</param>
        /// <param name="point">水印位置</param>
        public static void Watermark(List<string> loadPath, string text, Font font, Color color, PointF point, string savePath) 
        {
            loadPath.ForEach(x =>
            {
                Watermark(x,text, font, color, point, savePath);
            });
        }
    }
}
